let app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
   getMine:0,
   down:0,
   page:1,
   is_no_more:false,
   is_loading:false,
   count:0,
   total:1000

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var match_id = options.match_id;
    this.setData({
        match_id:match_id
    })
    this.setData({
      domain:app.globalData.domain,
      width:app.globalData.windowWidth,
      height:app.globalData.windowHeight,
      statusBarHeight:app.globalData.statusBarHeight,
      navHeight:app.globalData.navHeight,
      marginTop:app.globalData.navTitleTop
    });
   // that.getList('正在加载数据...')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    that.getList('正在加载数据...')
  },
  goBack:function(){
    wx.navigateBack();
  },
  getList: function (message){
    wx.showLoading({							
      title: message,
    })
    var that = this;
    var page=this.data.page;
    wx.request({
      url: app.globalData.domain+'wechat/face/getImage',
      data: {
          match_id:that.data.match_id,
          page:page
    },
      success:function(res){
        wx.hideLoading();
        console.log(res);
        var data= res.data;
        that.setData({
            listData:data.list,
            match:data.match
          //swiperList: contentlist.swiperList
        })
        ////console.log(that.data.matchListData)
      },
      fail:function(){
      }
    })
  },
  findMine(){
    let that=this;
    var user=this.data.user;
    var match_id=14;
    wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          // tempFilePath可以作为img标签的src属性显示图片
          console.log(res);
         var tempFilePaths=res.tempFiles[0].path;
         wx.showToast({
                icon:"loading",
                title: '正在上传',
            })      
          var timestamp=new Date().getTime(); 
          console.log(tempFilePaths)
          wx.uploadFile({
             filePath:tempFilePaths,
              name: 'file',
              url: app.globalData.domain+'wechat/face/find',
              formData:{
                match_id:match_id,
                },
                header: { 
                  'content-type':'application/x-www-form-urlencoded'       
                 },       
              success(res){
                console.log(res);
                var data=JSON.parse(res.data);
                console.log('=============');
               console.log(data);
               console.log('=============');
                if(data.code==0){              
                  //console.log(imageType)
                  that.setData({
                      list:data.list,
                      getMine:1
                  })
           
                }
                

              }
          
            })
          }
    })
  },
 setDown(e){
     console.log(e);
     this.setData({
         down:1
     })
 },
 cancleDown(e){
    console.log(e);
    var getMine = this.data.getMine;
    if(getMine==1){
        var list= this.data.list;
  //   var count = this.data.count;
     list.forEach(function(ele,index){
        
                list[index]['select'] = 0;
            
     })
     this.setData({
         list:list
     })
    }else{
        var listData= this.data.listData;
    
     listData.forEach(function(ele,index){
        listData[index]['select'] = 0;
     })
     this.setData({
        listData:listData,
       
    })
    }
    this.setData({
        down:0,
        count:0
    })   
 },
 gotoDown(){
    var getMine = this.data.getMine;
    var ids=[];
    if(getMine==1){
        var list= this.data.list;
  //   var count = this.data.count;
     list.forEach(function(ele,index){
        if(ele.select==1){
            ids.push(ele.id)
        }      
     }) 
    
    }else{
        var listData= this.data.listData;
    
     listData.forEach(function(ele,index){
        if(ele.select==1){
            ids.push(ele.id)
        } 
     })
    }
    //console.log(ids);
    var len=ids.length;
    if(len==0){
        wx.showLoading({
          title: '请选择图片',
        })
    }else{
        wx.navigateTo({
            url: 'downMore?id='+ids.toString(),
          })
    }
 },
 gotoDetail(e){
     var id=e.currentTarget.dataset.id;
     wx.navigateTo({
       url: 'detail?id='+id,
     })
 },
 selectImg(e){
     console.log(e);
     var id = e.currentTarget.dataset.id;
     var listData= this.data.listData;
     var count = this.data.count;
     listData.forEach(function(ele,index){
         console.log('==========');
         console.log('id='+id+'=>ele.id='+ele.id);
         console.log('index='+index);
         console.log('==========');
        if(ele.id == id){
            if(ele.select == 1){
                //ele.select = 0;
                count = count-1;
                listData[index]['select'] = 0;
            }else{
                count = count+1;
                listData[index]['select'] = 1;
            }
        }
     })
     console.log(listData);
     this.setData({
         listData:listData,
         count:count
     })
 },
 selectMine(e){
    console.log(e);
    var id = e.currentTarget.dataset.id;
    var list= this.data.list;
    var count = this.data.count;
    list.forEach(function(ele,index){
        console.log('==========');
        console.log('id='+id+'=>ele.id='+ele.id);
        console.log('index='+index);
        console.log('==========');
       if(ele.id == id){
           if(ele.select == 1){
               //ele.select = 0;
               count = count-1;
               listData[index]['select'] = 0;
           }else{
               count = count+1;
               listData[index]['select'] = 1;
           }
       }
    })
    console.log(list);
    this.setData({
        list:list,
        count:count
    })
},
gotoMine(){
    wx.reLaunch({
      url: '../mine/index',
    })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
      let that = this;
      let data = this.data;
      var is_loading = data.is_loading;
      var is_no_more = data.is_no_more;
    if (is_loading || is_no_more)
    return;
    is_loading = true;
    var page = data.page;
    page = page + 1;
    console.log(page);
    var match_id = data.match_id;
    wx.request({
        url: app.globalData.domain+'wechat/face/getImage',
        data: {
            match_id:match_id,
            page:page
      
        },
        success: function (res) {
            if (res.statusCode== 200) {

                var listData = that.data.listData.concat(res.data.list);
                that.setData({
                    listData: listData,
                    page:page
                });
                if (res.data.list.length == 0) {
                    is_no_more = true;
                }
            }
           
        },
        complete: function () {
            is_loading = false;
        }
    });
    },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})